package com.ljb.commlibrary.utils;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;

/**
 * 获取屏幕相关的工具类
 * Created by bin on 2017/6/24.
 */

public class ScreenUtils {
    private ScreenUtils() {
    }

    /**
     * 获取屏幕的宽度
     *
     * @param context 上下文
     * @return 屏幕的宽度, 单位px
     */
    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

    /**
     * 获取屏幕的高度
     *
     * @param context 上下文
     * @return 屏幕的高度
     */
    public static int getScreenHeight(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.heightPixels;
    }

    /**
     * 获取状态栏的高度
     *
     * @param context 上下文
     * @return 状态栏的高度
     */
    public static int getStatusHeight(Context context) {
        int statusHeight = -1;
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
            statusHeight = context.getResources().getDimensionPixelSize(height);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return statusHeight;
    }


    /*****************************获取屏幕截图相关工具********************************************/

    /**
     * 获取当前界面的屏幕截图,包含状态栏
     *
     * @param activity 当前界面的Activity
     * @return bitmap
     */
    public Bitmap getScreenShotWithStatusBar(Activity activity) {
        View decorView = activity.getWindow().getDecorView();
        decorView.setDrawingCacheEnabled(true);
        decorView.buildDrawingCache();
        Bitmap drawingCache = decorView.getDrawingCache();
        int width = getScreenWidth(activity);
        int height = getScreenHeight(activity);
        Bitmap b = Bitmap.createBitmap(drawingCache, 0, 0, width, height);
        decorView.destroyDrawingCache();
        return b;
    }

    /**
     * 获取当前界面的屏幕截图,不包含状态栏
     *
     * @param activity 当前界面的Activity
     * @return bitmap
     */
    public Bitmap getScreenShotWithoutStatusBar(Activity activity) {
        View decorView = activity.getWindow().getDecorView();
        decorView.setDrawingCacheEnabled(true);
        decorView.buildDrawingCache();
        Bitmap drawingCache = decorView.getDrawingCache();
        Rect rect = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        int statusBarHeight = rect.top;
        int width = getScreenWidth(activity);
        int height = getScreenHeight(activity);
        Bitmap b = Bitmap.createBitmap(drawingCache, 0, statusBarHeight, width, height - statusBarHeight);
        decorView.destroyDrawingCache();
        return b;
    }

    /**
     * 获取当前界面指定区域的屏幕截图
     *
     * @param activity 当前界面的Activity
     * @return bitmap
     */
    public static Bitmap getScreenShotWithoutCustomAarea(Activity activity, int x, int y, int width, int height) {
        View decorView = activity.getWindow().getDecorView();
        decorView.setDrawingCacheEnabled(true);
        decorView.buildDrawingCache();
        Bitmap drawingCache = decorView.getDrawingCache();
        Bitmap b = Bitmap.createBitmap(drawingCache, x, y, width, height);
        decorView.destroyDrawingCache();
        return b;
    }


    /**
     * 将控件尺寸大小转为当前设备下的像素大小(dp转px)
     *
     * @param dp 控件尺寸大小
     */
    public static int dp2px(Context context, int dp) {
        Resources res = context.getResources();
        float density = res.getDisplayMetrics().density;
        return (int) (dp * density + 0.5f);
    }

    /**
     * 将字体尺寸大小转为当前设备下的像素尺寸大小(sp转px)
     *
     * @param sp 字体的尺寸大小
     */
    public static float sp2px(Context context, int sp) {
        Resources res = context.getResources();
        float density = res.getDisplayMetrics().scaledDensity;
        return (int) (sp * density + 0.5f);
    }

}
